//index.js
let http = require('../../../utils/api.js');
let domain = require('../../../utils/api.js');
import {
    toData,
    myHttps,
    login
} from '../../../utils/util.js';
//获取应用实例
const app = getApp()
Page({
    data: {
        length: '',
        arr: [], //所有点赞
        hiddenLoading: false,
        users: '', //点赞得人
        hiddenLoading: false,
        userId: '', //用户id
        current: 1, //当前页面
        size: 10, //页容量
        winHeight: "", //窗口高度
        currentIndex: 0, //预设当前项的值
        scrollLeft: 0, //tab标题的滚动条位置
        informList: [], //系统通知
        commentList: [], //评论的通知列表
        topicsCommentList: [], //用户所有留言待审核列表
        informCount: '', //未读数量
        informListtotal: '',
        commentListtotal: '',
        topicsCommentListtotal: '',
        domain: domain.domain
    },
    // 滚动切换标签样式
    switchTab: function (e) {
        this.setData({
            informList: [], //系统通知
            commentList: [],
            topicsCommentList: [],
            currentIndex: e.detail.current,
            hiddenLoading: false,
        });
        // console.log(this.data.currentIndex)
        if (this.data.currentIndex == 0) { //系统通知
            this.xitonginform(this.data.userId, 1, 10); //系统通知列表
        } else if (this.data.currentIndex == 1) { //评论通知
            this.getcomment(this.data.userId, 1, 10); //获取用户评论通知的列表
        } else if (this.data.currentIndex == 2) { //待审核通知
            this.topicsComment(this.data.userId, 1, 10)
        }
        this.checkCor();
    },
    // 点击标题切换当前页时改变样式
    swichNav: function (e) {
        var cur = e.target.dataset.current;
        this.setData({
            currentIndex: cur
        })
        if (this.data.currentIndex == 0) { //系统通知
            this.xitonginform(this.data.userId, 1, 10); //系统通知列表
        } else if (this.data.currentIndex == 1) { //评论通知
            this.getcomment(this.data.userId, 1, 10); //获取用户评论通知的列表
        } else if (this.data.currentIndex == 2) { //待审核通知
            this.topicsComment(this.data.userId, 1, 10)
        }

    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function () {
        if (this.data.currentIndex > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    // 获取未读通知数量
    getinformCount() {
        let url = http.api.informCount + this.data.userId + '/messages/count'
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    informCount: res.data,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        this.setData({
            userId: e.userId
        })
        // console.log(e)
        this.xitonginform(e.userId, this.data.current, this.data.size); //系统通知列表

        this.getinformCount(); //系统数量
        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function (res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR - 120;
                // console.log(calc)
                that.setData({
                    winHeight: calc
                });
            }
        });
    },
    // 用户系统通知列表
    xitonginform(userId, page, size) {
        let url = http.api.xitonginform + userId + '/messages?page=' + page + '&size=' + size;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].update_time = toData(res.data.content[i].update_time)
                    // this.data.informList.push(res.data.content[i])
                }
                this.setData({
                    informList: this.data.informList.concat(res.data.content),
                    hiddenLoading: true,
                    hideHeader: true,
                    informListtotal: res.data._meta.total,
                })
                // console.log(this.data.informList)
            }
        }).catch(err => { })
    },
    //   获取用户评论通知列表
    getcomment(id, page, size) {
        let url = http.api.commentno + id + '/comment_notices?page=' + page + '&size=' + size;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].content = JSON.parse(res.data.content[i].content)
                    res.data.content[i].update_time = toData(res.data.content[i].update_time)
                    // this.data.commentList.push(res.data.content[i])
                }
                this.setData({
                    commentList: this.data.commentList.concat(res.data.content),
                    hiddenLoading: true,
                    hideHeader: true,
                    commentListtotal: res.data._meta.total,
                })
            }

        }).catch(err => { })
    },
    // 获取用户待审核列表
    topicsComment(id, page, size) {
        this.setData({
            topicsCommentList: [],
        })
        let url = http.api.topicsComment + id + '/topics/comments?page=' + page + '&size=' + size;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].update_time = toData(res.data.content[i].update_time)
                    // this.data.topicsCommentList.push(res.data.content[i])
                }
                this.setData({
                    topicsCommentList: this.data.topicsCommentList.concat(res.data.content),
                    hiddenLoading: true,
                    hideHeader: true,
                    topicsCommentListtotal: res.data._meta.total,
                })
            }

        }).catch(err => { })
    },
    // 点击 忽略或通过走接口
    checkMsg(id, status, index) {
        let url = http.api.checkMsg + id + '/topic'
        var that = this
        let obj = {
            status: status
        }
        myHttps({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                this.data.topicsCommentList.splice(index, 1)
                this.data.informCount.pendingNotice = this.data.informCount.pendingNotice - 1
                this.setData({
                    topicsCommentList: this.data.topicsCommentList,
                    informCount: this.data.informCount
                })
            }
        }).catch(err => { })
    },
    // 点击忽略
    neglect(e) {
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        this.checkMsg(id, 'Invalid', index)
    },
    // 点击通过
    pass(e) {
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        this.checkMsg(id, 'Approved', index)
    },
    // 下拉刷新
    refresh: function (e) {
        // console.log('下拉刷新')
        var that = this;
        setTimeout(function () {
            var date = new Date();
            that.setData({
                informList: [],
                commentList: [],
                topicsCommentList: [],
                size: 10,
                current: 1,
                hiddenLoading: false,
                scrollTop: 0,
                refreshTime: date.toLocaleTimeString(),
                hideHeader: false
            })
            if (that.data.currentIndex == 0) { //系统通知
                that.xitonginform(that.data.userId, that.data.current, that.data.size); //系统通知列表
                // console.log(that.data.informCount.systemNotice)
                that.data.informCount.systemNotice = 0
                that.setData({
                    informCount: that.data.informCount
                })
            } else if (that.data.currentIndex == 1) { //评论通知
                that.getcomment(that.data.userId, that.data.current, that.data.size); //获取用户评论通知的列表
                that.data.informCount.commentNotice = 0
                that.setData({
                    informCount: that.data.informCount
                })
            } else if (that.data.currentIndex == 2) { //待审核通知
                that.topicsComment(that.data.userId, that.data.current, that.data.size)
            }
        }, 300);
    },
    // 上拉加载更多
    loadMore: function () {
        // console.log('上拉')
        var that = this;
        setTimeout(function () {
            // console.log(that.data.currentTab)
            if (that.data.currentIndex == 0) { //系统通知
                that.setData({
                    current: that.data.current + 1,
                    size: 10,
                })
                // console.log(that.data.informListtotal)
                if (that.data.informListtotal > 10) {
                    console.log(that.data.informListtotal)
                    that.xitonginform(that.data.userId, that.data.current, that.data.size); //系统通知列表
                }
            } else if (that.data.currentIndex == 1) { //评论通知
                that.setData({
                    current: that.data.current + 1,
                    size: 10,
                })
                console.log(that.data.commentListtotal)
                if (that.data.commentListtotal > 10) {
                    console.log(that.data.commentListtotal)
                    that.getcomment(that.data.userId, that.data.current, that.data.size); //获取用户评论通知的列表
                }
            } else if (that.data.currentIndex == 2) { //待审核通知
                that.setData({
                    current: that.data.current + 1,
                    size: 10,
                })
                console.log(that.data.topicsCommentListtotal)
                if (that.data.topicsCommentListtotal > 10) {
                    console.log(that.data.topicsCommentListtotal)
                    that.topicsComment(that.data.userId, that.data.current, that.data.size)
                }
            }

        }, 300);
    },

})